/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Clientes;

//import java.sql.SQLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class BusquedaCliente extends javax.swing.JInternalFrame {
        
    public String consulta ;
    public java.sql.ResultSet rs ;
    public ClientesBD procCliente = new ClientesBD("localhost","Certcosa","root","root");
    
    public BusquedaCliente() {
        initComponents();
        rNombre.setSelected(true);
        consulta = "WHERE NombreCliente LIKE '%";
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tCliente = new javax.swing.JTable();
        tBusqueda = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        rNombre = new javax.swing.JRadioButton();
        rNit = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/preview_128.png"))); // NOI18N

        tCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nombre", "NIT", "Telefono", "Dirección"
            }
        ));
        jScrollPane1.setViewportView(tCliente);

        jLabel4.setText("jLabel4");

        jLabel2.setText("Buscar Por:");

        rNombre.setText("Nombre");
        rNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rNombreActionPerformed(evt);
            }
        });

        rNit.setText("NIT");
        rNit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rNitActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/search_08_24.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Bright", 1, 18)); // NOI18N
        jLabel1.setText("Búsqueda de Clientes");

        jButton2.setText("Nuevo");

        jButton3.setText("Editar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel3))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(335, 335, 335)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(154, 154, 154)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 654, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(45, 45, 45)
                                .addComponent(rNombre)
                                .addGap(63, 63, 63)
                                .addComponent(rNit))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(74, 74, 74)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rNombre)
                    .addComponent(rNit))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jButton1))
                .addGap(65, 65, 65)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addGap(259, 259, 259)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rNitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rNitActionPerformed
        if(rNit.isSelected()){
         rNombre.setSelected(false);
         consulta = "WHERE NitCliente LIKE '%";
        }
    }//GEN-LAST:event_rNitActionPerformed

    private void rNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rNombreActionPerformed
        if(rNombre.isSelected()){
         rNit.setSelected(false);
         consulta = "WHERE NombreCliente LIKE '%";
        }
        
    }//GEN-LAST:event_rNombreActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("NIT Cliente");
    tableHeaders.add("Nombre"); 
    tableHeaders.add("Direccion");
    tableHeaders.add("Telefono");
    
       
        try {
          System.out.println("cosulta"+ consulta+tBusqueda.getText()+"%'");
          rs = procCliente.VerdatosCliente(consulta+tBusqueda.getText()+"%'");
          while (rs.next()){
             
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(rs.getInt("NitCliente"));
        oneRow.add(rs.getString("NombreCliente"));
        oneRow.add(rs.getString("DireccionCliente"));
        oneRow.add(rs.getString("TelefonoCliente"));
        tableData.add(oneRow);
       }
        tCliente.setModel(new DefaultTableModel(tableData, tableHeaders));
        
       
        
        
    
        } catch (SQLException ex) {
            Logger.getLogger(BusquedaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
//     java.sql.ResultSet res2 =null;
//     ClientesBD procs ClientesBD();
//        Clientes formEditar= new Clientes(1);
//            formEditar.setTitle("Edición Cliente");
//            formEditar.setVisible(true);
//            
//         
//        try {
//            
//            res2 = procs.VerdatosCliente(tCliente.getValueAt(tCliente.getSelectedRow(),0).toString());
//            formEditar.verResultsetObtenerDatosCliente(res2,tCliente.getValueAt(tCliente.getSelectedRow(),0).toString());
//            
//        } catch (SQLException ex) {
    }//GEN-LAST:event_jButton3ActionPerformed

    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rNit;
    private javax.swing.JRadioButton rNombre;
    private javax.swing.JTextField tBusqueda;
    private javax.swing.JTable tCliente;
    // End of variables declaration//GEN-END:variables
}
